package org.millenaire_reborn.datagen;

import net.minecraft.client.data.models.BlockModelGenerators;
import net.minecraft.client.data.models.ItemModelGenerators;
import net.minecraft.client.data.models.ModelProvider;
import net.minecraft.client.data.models.model.ModelTemplates;
import net.minecraft.data.PackOutput;
import net.neoforged.neoforge.registries.DeferredItem;
import org.millenaire_reborn.MillenaireReborn;
import org.millenaire_reborn.annotations.AutoItem;
import org.millenaire_reborn.init.ModItems;

import java.lang.reflect.Field;

public class ModItemModelProvider extends ModelProvider {

    public ModItemModelProvider(PackOutput output) {
        super(output, MillenaireReborn.MOD_ID);
    }

    @Override
    protected void registerModels(BlockModelGenerators blockModels, ItemModelGenerators itemModels) {
        for (Field field: ModItems.class.getFields()) {
            var anno = field.getAnnotation(AutoItem.class);
            if (anno != null) {
                try {
                    var items = field.get(field.getDeclaringClass());
                    if (anno.type().equals("item")) {
                        itemModels.generateFlatItem(((DeferredItem<?>) items).get(), ModelTemplates.FLAT_ITEM);
                    }
                    if (anno.type().equals("tool")) {
                        itemModels.generateFlatItem(((DeferredItem<?>) items).get(), ModelTemplates.FLAT_HANDHELD_ITEM);
                    }
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
